package com.lsq.tucao.express.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsq.tucao.express.bean.po.HeartDetail;
import com.lsq.tucao.express.bean.po.TucaoDetail;
import com.lsq.tucao.express.bean.po.UserLike;
import com.lsq.tucao.express.dao.HeartDetailMapper;
import com.lsq.tucao.express.dao.TucaoDetailMapper;
import com.lsq.tucao.express.dao.UserLikeMapper;
import com.lsq.tucao.express.response.JsonResult;
import com.lsq.tucao.express.service.ManageMyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManageMyServiceImpl implements ManageMyService {

    @Autowired
    private TucaoDetailMapper tucaoDetailMapper;
    @Autowired
    private HeartDetailMapper heartDetailMapper;
    @Autowired
    private UserLikeMapper userLikeMapper;

    @Override
    public JsonResult getMyTucao(Integer current, Integer size, String username) {
        if(!StringUtils.isEmpty(username)){
            Page<TucaoDetail> page = new Page<>(current,size);
            QueryWrapper<TucaoDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(TucaoDetail::getIsDelete,1)
                    .eq(TucaoDetail::getUsername,username)
                    .orderByDesc(TucaoDetail::getCreateTime);

            Page<TucaoDetail> tucaoDetailPage = tucaoDetailMapper.selectPage(page, queryWrapper);
            if(tucaoDetailPage != null){
                return new JsonResult(tucaoDetailPage,200,"分页查询我发布的吐槽数据成功");
            }else {
                return new JsonResult(500,"分页查询我发布的吐槽数据失败");
            }
        }else {
            return new JsonResult(500,"请传入当前登录用户的用户名");
        }
    }

    @Override
    public JsonResult getMyHeart(Integer current, Integer size, String username) {
        if(!StringUtils.isEmpty(username)){
            Page<HeartDetail> page = new Page<>(current,size);
            QueryWrapper<HeartDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(HeartDetail::getIsDelete,1)
                    .eq(HeartDetail::getUsername,username)
                    .orderByDesc(HeartDetail::getCreateTime);

            Page<HeartDetail> heartDetailPage = heartDetailMapper.selectPage(page, queryWrapper);
            if(heartDetailPage != null){
                return new JsonResult(heartDetailPage,200,"分页查询我发布的鸡汤数据成功");
            }else {
                return new JsonResult(500,"分页查询我发布的鸡汤数据失败");
            }
        }else {
            return new JsonResult(500,"请传入当前登录用户的用户名");
        }
    }

    @Override
    public JsonResult getMyLike(Integer current, Integer size, Long userId) {
        if(userId != null){
            Page<UserLike> page = new Page<>(current,size);
            QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(UserLike::getUserId,userId)
                    .orderByDesc(UserLike::getCreateTime);

            Page<UserLike> userLikePage = userLikeMapper.selectPage(page, queryWrapper);
            if(userLikePage != null){
                return new JsonResult(userLikePage,200,"分页查询我的点赞数据成功");
            }else {
                return new JsonResult(500,"分页查询我的点赞数据失败");
            }
        }else {
            return new JsonResult(500,"请传入当前登录用户的用户id");
        }
    }

    @Override
    public JsonResult delMyTucao(Long tucaoId) {
        if(tucaoId != null){
            UpdateWrapper<TucaoDetail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .eq(TucaoDetail::getTucaoId,tucaoId)
                    .set(TucaoDetail::getIsDelete,0);

            int update = tucaoDetailMapper.update(null, updateWrapper);
            if (update > 0){
                return new JsonResult(200,"删除成功");
            }else {
                return new JsonResult(500,"删除失败");
            }
        }else {
            return new JsonResult(500,"请传入要删除的吐槽id");
        }
    }

    @Override
    public JsonResult delMyHeart(Long heartId) {
        if(heartId != null){
            UpdateWrapper<HeartDetail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .eq(HeartDetail::getId,heartId)
                    .set(HeartDetail::getIsDelete,0);

            int update = heartDetailMapper.update(null, updateWrapper);
            if (update > 0){
                return new JsonResult(200,"删除成功");
            }else {
                return new JsonResult(500,"删除失败");
            }
        }else {
            return new JsonResult(500,"请传入要删除的吐槽id");
        }
    }

}
